package com.haha;

import lombok.extern.slf4j.Slf4j;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
@Slf4j
public class nettystudy {
    public static void main(String[] args) {
        try(FileChannel channel=new FileInputStream("E:\\gulimail\\nettystudy\\data.txt").getChannel()) {
            //准备缓冲区
            ByteBuffer buffer = ByteBuffer.allocate(10);
            while(true){
                //从channel读取数据,向缓冲区写入
                int len = channel.read(buffer);
                log.debug("读取到的字节数{}",len);
                if(len==-1){
                    break;
                }
                //打印buffer内容
                buffer.flip();//切换至读的模式
                while(buffer.hasRemaining()){//是否还有剩余的、
                    byte b = buffer.get();
                    log.debug("实际的字节数{}",len);
                }
                buffer.clear();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
